#include "stm32f2xx.h"
#include "abs_encoder.h"

__IO uint16_t encoder_value[ENC_LEN];

void abs_encoder_dma_config(void)
{
	DMA_InitTypeDef DMA_InitStructure;
    RCC_AHB1PeriphClockCmd(	ENC_DMA_CLK, ENABLE);	
    //reference manual: 173/1317	 DMA_Channel_0->DMA2_Stream0
	DMA_InitStructure.DMA_Channel = DMA_Channel_0;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&encoder_value;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = 50;
    /*外设地址寄存器不递增，只读当前寄存器*/
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    /*存储器地址递增*/
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    /*外设数据长度*/
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    /*DMA存储器长度*/
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    /*DMA流进行循环操作*/
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    /*DMA通道优先级高*/
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    /*选择直接模式，而不是FIFO模式*/
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;   
    /*FIFO阈值等级*/    
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    /*DMA存储器突发传输，只有在地址递增模式下才有效*/
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    /*外设突发传输，只有在地址递增模式下才有效*/
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA2_Stream0, &DMA_InitStructure);
	DMA_Cmd(DMA2_Stream0, ENABLE);
}


void abs_encoder_gpio_config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(	ENC_ADC_GPIO_CLK, ENABLE);	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	
	GPIO_InitStructure.GPIO_Pin  =ENC_ADC_PIN;
	GPIO_Init(ENC_ADC_GPIO_PORT,&GPIO_InitStructure);
}

void abs_encoder_adc_config(void)
{    
    ADC_InitTypeDef ADC_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    RCC_APB2PeriphClockCmd(	ENC_ADC_CLK, ENABLE);			
	ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
	ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
	ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
	ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
	ADC_CommonInit(&ADC_CommonInitStructure);
	ADC_InitStructure.ADC_Resolution =ENC_ADC_RESOLUTION;
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;		
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;   
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
	ADC_InitStructure.ADC_NbrOfConversion = 1; 
	ADC_Init(ENC_ADC, &ADC_InitStructure);   
	ADC_RegularChannelConfig(ENC_ADC, ENC_ADC_CHANNEL, 1, ADC_SampleTime_480Cycles);
	ADC_DMARequestAfterLastTransferCmd(ENC_ADC, ENABLE);
	ADC_DMACmd(ENC_ADC, ENABLE); 
	ADC_Cmd(ENC_ADC, ENABLE);
	ADC_SoftwareStartConv(ENC_ADC);

}

void abs_encoder_init(void)
{
    abs_encoder_gpio_config();
    abs_encoder_adc_config();
    abs_encoder_dma_config();
}

float get_abs_encoder()
{
    int i = 0;
    int sum = 0;
    float value  = 0.0;
    for(; i < ENC_LEN; i++)
    {
       sum += encoder_value[i];
    }
    value = (float)sum/(float)ENC_LEN;
    value = value/4096.0*360.0;
   //ORG return value ;
    // DEBUG
    return 0.0;
}

